package com.example.justbuy.action.buyer;

import com.example.justbuy.pojo.Goods;
import com.example.justbuy.pojo.ShoppingCart;
import com.example.justbuy.pojo.User;
import com.example.justbuy.service.ShoppingCartMapperService;

import com.example.justbuy.util.ConvertDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Pattern;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ShoppingCartAction {
    @Autowired
    private ShoppingCartMapperService shoppingCartMapperService;

    /**
     * 查询所有。跳转到我的购物车
     * */
    @RequestMapping("show_all.htm")
    public  String showAll( Model md, HttpServletRequest request, HttpServletResponse response){
        User user= (User)request.getSession().getAttribute("user");
        List<ShoppingCart> list= shoppingCartMapperService.getAllShoppingCartByuserId(user.getId());
      /* list.get(0).getGoods().size();
        System.out.println(list.get(0).getGoods().size());*/
        md.addAttribute("list",list);
        return "Shopping_cart.html";
    }
    /*
    * 删除单条信息
    * 删除后返回购物车
    * **/
    @RequestMapping("del_one.htm")
    public @ResponseBody String  deleOne(Integer goodsid, Model md, HttpServletRequest request, HttpServletResponse response){
        int flag=shoppingCartMapperService.deleShoppingCartByGoodsId(goodsid);
        if(flag>0){
            return "1";
        }else{
            return "2";
        }
    }
    /*
     *清空购物车
     * */
    @RequestMapping("del_all.htm")
    public  String deleAll(int userid, Model md, HttpServletRequest request, HttpServletResponse response){
        User user= (User)request.getSession().getAttribute("user");
        int flag=shoppingCartMapperService.deleAllOfShoppingCartByUserId(user.getId());
        if(flag>0){
            System.out.println("购物车已经清空！");
            return "Shopping_cart.html";
        }else{
            System.out.println("系统出错");
            return "mine.html";
        }
    }
    /*
    *购物车单条信息
    *
    * **/
    @RequestMapping("select_one.htm")
    public  String selectOne(int goodsId,String model,
                          Model md, HttpServletRequest request, HttpServletResponse response) {
        User user = (User) request.getSession().getAttribute("user");
//        //获取所有
//        List<ShoppingCart> list1 = shoppingCartMapperService.getAllShoppingCartByuserId(user.getId());
//        boolean flag = true;
//        for (int a = 0; a < list1.size(); a++) {
//            if (list1.get(a).getGoodsid() == goodsId) {
//                HashMap mp = new HashMap();
//                mp.put("id", list1.get(a).getId());
//                mp.put("amount", list1.get(a).getAmount() + 1);
//                mp.put("money", (list1.get(a).getMoney() / list1.get(a).getAmount()) * (list1.get(a).getAmount() + 1));
//                int fg = shoppingCartMapperService.updateOfShoppingCartBygoodsId(mp);
//                if (fg > 0) {
//                    flag = false;
//                }
//            }
//        }
//        if (flag) {
//            HashMap map = new HashMap();
//            Goods goods = shoppingCartMapperService.getTheGoodsById(goodsId);
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//            String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
//            map.put("goodsId", goodsId);
//            map.put("addTime", ConvertDateUtil.castDate(date));
//            map.put("userId", user.getId());
//            map.put("modles", model);
//            map.put("storeid", goods.getStoreId());
//            map.put("amount", 1);
//            map.put("money", goods.getPrice());
//            int flag1 = shoppingCartMapperService.addOneOfInfoToShoppingCartByGoodsId(map);
//        }
        List<ShoppingCart> list = shoppingCartMapperService.getAllShoppingCartByuserId(user.getId());
        md.addAttribute("list", list);
        // return "redirect:show_all.htm";
        return "Shopping_cart.html";
    }

    /*购物车添加单条信息*/
    @RequestMapping("add_one.htm")
    public  @ResponseBody  int addOne(int goodsId,String model,
                          Model md, HttpServletRequest request, HttpServletResponse response) {
        User user = (User) request.getSession().getAttribute("user");
        //获取所有
        List<ShoppingCart> list1 = shoppingCartMapperService.getAllShoppingCartByuserId(user.getId());
        boolean flag = true;
        int flag2=0;
        for (int a = 0; a < list1.size(); a++) {
            if (list1.get(a).getGoodsid() == goodsId) {
                HashMap mp = new HashMap();
                mp.put("id", list1.get(a).getId());
                mp.put("amount", list1.get(a).getAmount() + 1);
                mp.put("money", (list1.get(a).getMoney() / list1.get(a).getAmount()) * (list1.get(a).getAmount() + 1));
                int fg = shoppingCartMapperService.updateOfShoppingCartBygoodsId(mp);
                if (fg > 0) {
                    flag = false;
                    flag2=1;
                }
            }
        }
        if (flag) {
            HashMap map = new HashMap();
            Goods goods = shoppingCartMapperService.getTheGoodsById(goodsId);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
            map.put("goodsId", goodsId);
            map.put("addTime", ConvertDateUtil.castDate(date));
            map.put("userId", user.getId());
            map.put("modles", model);
            map.put("storeid", goods.getStoreId());
            map.put("amount", 1);
            map.put("money", goods.getPrice());
            int flag1 = shoppingCartMapperService.addOneOfInfoToShoppingCartByGoodsId(map);
            flag2=1;
        }
       return flag2;
    }

    /*异步接受购物车信息，并修改购物车表*/
    @RequestMapping("shoppingCal.htm")
    public@ResponseBody  String  updateShoppingCart(Integer goodsid, Integer amount, Double money,
                               Model md, HttpServletRequest request, HttpServletResponse response){
        HashMap map=new HashMap();
        map.put("id",goodsid);
        map.put("amount",amount);
        map.put("money",money);
        int flag=shoppingCartMapperService.updateOfShoppingCartBygoodsId(map);
       // System.out.println("111:"+flag);
        if(flag>0){
            return "1";
            //修改成功，返回1
        }else{
            return  "2";
            //修改失败，返回2
        }
    }
    /**
     * 根据goodsid获取购物车单个商品总价 异步返回到前端
     */
    @RequestMapping("shopChang.htm")
    public@ResponseBody String shopChang(int goodsid,HttpServletRequest request){
        User user= (User)request.getSession().getAttribute("user");
        HashMap map=new HashMap();
        //map.put("userId",Integer.valueOf(user.getId()));
        map.put("userId",user.getId());
        map.put("id",Integer.valueOf(goodsid));

        Double cc=shoppingCartMapperService.getAllpriceByGoodsid(map);
        //System.out.println(cc);
        return String.valueOf(cc);
    }
    @RequestMapping("shopChang1.htm")
    public@ResponseBody String shopChang1(String goodsid,HttpServletRequest request){
        User user= (User)request.getSession().getAttribute("user");
        Double cc=Double.valueOf(0);
        if(goodsid!=null&&goodsid!=""){
            String[] str= goodsid.split(",");
            for(int i=0;i<str.length;i++){
                HashMap map=new HashMap();
                //map.put("userId",Integer.valueOf(user.getId()));
                map.put("userId",user.getId());
                map.put("id",Integer.valueOf(str[i]));
                cc=cc+shoppingCartMapperService.getAllpriceByGoodsid(map);
            }

        }
      //  System.out.println(cc);
        return String.valueOf(cc);
    }
}
